﻿package idv.cjcat.emitter.fields {
	use namespace engine;
	
	import flash.errors.*;
	
	import idv.cjcat.emitter.*;
	import idv.cjcat.emitter.geom.*;
	
	/**
	 * Abstract 2D field.
	 * <p>
	 * This is an abstarct class and should not be instantiated.
	 * </p>
	 */
	public class Field implements IEmitterXML, IEmitterDestructible {
		
		public var active:Boolean = true;
		public var massless:Boolean = true;
		
		public function Field() {
			this.id = null;
		}
		
		/**
		 * This is an abstract method and should be overriden
		 */
		public function getMotionData2D(pd:ParticleData):MotionData2D {
			return null;
		}
		
		
		//IEmitterDestructible
		//------------------------------------------------------------------------------------------------
		
		engine var _destroyed:Boolean = false;
		public function get destroyed():Boolean { return _destroyed }
		
		//------------------------------------------------------------------------------------------------
		//end of IEmitterDestructible
		
		
		//XML
		//------------------------------------------------------------------------------------------------
		
		/** @private */
		engine var _id:String;
		public function get id():String { return _id; }
		public function set id(value:String):void {
			delete EmitterGlobal._fields[_id];
			if (value == null) {
				while (EmitterGlobal._fields["field" + EmitterGlobal._fieldCounter] != undefined) {
					EmitterGlobal._fieldCounter++;
				}
				_id = "field" + EmitterGlobal._fieldCounter;
			} else {
				if (EmitterGlobal._fields[value] == undefined) {
					_id = value;
				} else {
					throw new IllegalOperationError("This name is already used by another field.");
				}
			}
			EmitterGlobal._fields[_id] = this;
		}
		public function toXML():XML {
			var xml:XML = <field/>;
			xml.@id = _id;
			xml.@type = "abstract";
			xml.@active = active.toString();
			xml.@massless = massless.toString();
			
			return xml;
		}
		public function parseXML(xml:XML):void {
			active = (xml.@active == "true");
			massless = (xml.@massless == "true");
		}
		
		//------------------------------------------------------------------------------------------------
		//end of XML
	}
}